package com.yuexun.dhdoctor.module.Contract.ui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.Contract.adapter.GroupMainFragmentPagerAdapter;
import com.yuexun.dhdoctor.module.Contract.ui.fragment.Frag_Department;
import com.yuexun.dhdoctor.module.Contract.ui.fragment.Frag_Group;
import com.yuexun.dhdoctor.module.Contract.ui.fragment.Frag_People;

import org.kymjs.kjframe.ui.SupportFragment;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;

/**
 * Created by work-jx on 2016/7/17.
 */
public class Frag_Contract extends SupportFragment {
    private ViewPager mPager;
    private TextView groupTitle;
    private TextView depTitle;
    private TextView contactTitle;
    private TextView select_hospital;
    private TextView hospitalNameTxt;
    private LinearLayout allTitle;
    private ArrayList<Fragment> fragmentsList = new ArrayList<>();
    Frag_Department departmentActivity;
    Frag_Group groupActivity;
    Frag_People peopleActivity;

    String hospitalName;
    public static int hospitalId;
    MyBroadcastReceiver mBroadcastReceiver;

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        return null;
    }

    public class MyBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            hospitalName = intent.getExtras().getString("hospitalName");
            hospitalId = intent.getExtras().getInt("hospitalId");
            hospitalNameTxt.setText(hospitalName);
            if (hospitalId == PreferenceHelper.readInt(getActivity(), AppConfig.USER_INFO, AppConfig.HOSPITALID)) {
                Drawable drawable = getResources().getDrawable(R.drawable.icon_this_court);
                /// 这一步必须要做,否则不会显示.
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                select_hospital.setCompoundDrawables(drawable, null, null, null);
                hospitalNameTxt.setVisibility(View.GONE);
            } else {
                Drawable drawable = getResources().getDrawable(R.drawable.icon_other_court);
                /// 这一步必须要做,否则不会显示.
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                select_hospital.setCompoundDrawables(drawable, null, null, null);
                hospitalNameTxt.setVisibility(View.VISIBLE);
            }
//            if(mPager.getCurrentItem() == 0){
//                peopleActivity.setHospitalId(hospitalId);
//            }else if(mPager.getCurrentItem() == 1){
//
//            }else{
//
//            }
            peopleActivity.setHospitalId(hospitalId);
            departmentActivity.setHospitalId(hospitalId);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_contract, null);
        hospitalName = PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.HOSPITALNAME);
        hospitalId = PreferenceHelper.readInt(getActivity(), AppConfig.USER_INFO, AppConfig.HOSPITALID);
        initView(view);
        initData(view);
        initMonitor();

        mBroadcastReceiver = new MyBroadcastReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.MyBroadcastReceiver");
        getActivity().registerReceiver(mBroadcastReceiver, intentFilter);

        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(mBroadcastReceiver);
    }

    private void initView(View parentView) {
        mPager = (ViewPager) parentView.findViewById(R.id.group_main_ViewPager);
        allTitle = (LinearLayout) parentView.findViewById(R.id.allTitle);
        allTitle.setVisibility(View.VISIBLE);
        contactTitle = (TextView) parentView.findViewById(R.id.contactTitle);
        depTitle = (TextView) parentView.findViewById(R.id.depTitle);
        groupTitle = (TextView) parentView.findViewById(R.id.groupTitle);
        select_hospital = (TextView) parentView.findViewById(R.id.select_hospital);
        hospitalNameTxt = (TextView) parentView.findViewById(R.id.txt_hospitalName);
    }

    private void initData(View parentView) {
        // 不确定滑动到其他标签页时此处3个页面是否有被释放
        fragmentsList.clear();
        departmentActivity = new Frag_Department();
        groupActivity = new Frag_Group();
        peopleActivity = new Frag_People();
        fragmentsList.add(peopleActivity);
        fragmentsList.add(departmentActivity);
        fragmentsList.add(groupActivity);
        InitViewPager(parentView);
//        positionValue = new boolean[mFragments.size()];
//        for (int i = 0; i < positionValue.length; i++) {
//            positionValue[i] = false;
//        }
        depTitle.setSelected(true);
    }

    /**
     * 初始化ViewPager
     */
    private void InitViewPager(View parentView) {
        mPager.getContext();
        mPager.setPageMargin(2);// 设置页面边缘像素，不设置下面的贴图不会起作用
        mPager.setPageMarginDrawable(android.R.drawable.divider_horizontal_bright);
        mPager.setOffscreenPageLimit(3);
        mPager.setAdapter(new GroupMainFragmentPagerAdapter(getChildFragmentManager(), fragmentsList));
        mPager.setCurrentItem(1);// 设置选中哪个item
    }

    private void initMonitor() {
        groupTitle.setOnClickListener(new Click());
        depTitle.setOnClickListener(new Click());
        contactTitle.setOnClickListener(new Click());
        select_hospital.setOnClickListener(new Click());
        mPager.setOnPageChangeListener(new MyOnPageChangeListener());
    }

    class Click implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.groupTitle:
                    mPager.setCurrentItem(0);
//                    groupActivity.toTheTop(v);
                    groupTitle.setSelected(true);
                    depTitle.setSelected(false);
                    contactTitle.setSelected(false);
                    break;
                case R.id.depTitle:
                    mPager.setCurrentItem(1);
//                    departmentActivity.toTheTop(v);
                    groupTitle.setSelected(false);
                    depTitle.setSelected(true);
                    contactTitle.setSelected(false);
                    break;
                case R.id.contactTitle:
                    mPager.setCurrentItem(2);
//                    peopleActivity.toTheTop(v);
                    groupTitle.setSelected(false);
                    depTitle.setSelected(false);
                    contactTitle.setSelected(true);
                    break;
                case R.id.select_hospital:
                    Bundle bundle = new Bundle();
                    Intent intent = new Intent(getActivity(), Act_Hospital_Select.class);
                    bundle.putString("newHospital", hospitalName);
                    bundle.putInt("newHospitalId", hospitalId);
                    bundle.putInt("strHospitalId", PreferenceHelper.readInt(getActivity(), AppConfig.USER_INFO, AppConfig.HOSPITALID));
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                default:
                    break;
            }
        }
    }

    /**
     * 页卡切换监听
     */
    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageSelected(int arg0) {
            if (arg0 == 0) {
                groupTitle.setSelected(true);
                depTitle.setSelected(false);
                contactTitle.setSelected(false);
//                if (positionValue[arg0] == false) {
//                    groupActivity.updateWidget(GroupItem.GROUP_LIST, userid, 0);
//                    positionValue[arg0] = true;
//                }
            } else if (arg0 == 1) {
                groupTitle.setSelected(false);
                depTitle.setSelected(true);
                contactTitle.setSelected(false);

            } else if (arg0 == 2) {
                groupTitle.setSelected(false);
                depTitle.setSelected(false);
                contactTitle.setSelected(true);
//                if (positionValue[arg0] == false) {
//
//                    contactActivity.updateWidget();
//                    positionValue[arg0] = true;
//                }
            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // 只有当arg1和arg2都为0时表示滑动结束
            if (arg1 == 0.0 && arg2 == 0) {
//                pageNo = arg0;
//                Logger.i("logger", "当前页面" + pageNo);
            }
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    }

}
